package com.atguigu.tingshu;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@EnableDiscoveryClient
@EnableFeignClients
@EnableAsync
public class ServiceSearchApplication implements CommandLineRunner {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private ElasticsearchClient elasticsearchClient;

    public static void main(String[] args) {
        SpringApplication.run(ServiceSearchApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        RBloomFilter<Object> filter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        filter.tryInit(100000,0.001);
        SearchResponse<AlbumInfoIndex> search = elasticsearchClient.search(
                s -> s.index("albuminfo").size(10000)
                , AlbumInfoIndex.class);
        List<Hit<AlbumInfoIndex>> hitList = search.hits().hits();
        List<AlbumInfoIndex> collect = hitList.stream().map(Hit::source).collect(Collectors.toList());
        collect.forEach(item-> filter.add(item.getId()));
    }
}
